package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.dto.NurseWorkerInfoRequestDto;
import com.blueocean_health.care.management.domain.dto.NurseWorkerInfoResponseDto;
import com.blueocean_health.care.management.domain.dto.RequestIdDto;
import com.blueocean_health.care.management.transfer.client.NurseWorkerService;

@RestController
@RequestMapping(value = "/nurse", produces = MediaType.APPLICATION_JSON_VALUE)
public class NurseWorkerInfoController {
	@Autowired
	private NurseWorkerService nurseWorkerService;

	@Value("classpath:template/护工模板.xlsx")
	private Resource resource;
	@Value("${picture.filePath.nurse.pdf}")
	private String PDFPICTUREPATH;
	@Value("${service.address.nurse.pdf}")
	private String pdfServiceAddress;

	/**
	 * 获取护工详情
	 */
	@RequestMapping("/query")
	public SysResult findDetail(@RequestBody RequestIdDto requestId) {
		return nurseWorkerService.findDetail(requestId);
	}

	/**
	 * 获取护工列表分页
	 */
	@RequestMapping("/list")
	public SysResult findList(@RequestBody NurseWorkerInfoRequestDto nurseWorkerInfoRequestDto) {
		if (StringUtils.isEmpty(nurseWorkerInfoRequestDto)) {
			return SysResult.error("参数不能为空");
		}
		if (StringUtils.isEmpty(nurseWorkerInfoRequestDto.getTeamId())) {
			return SysResult.error("医院Id不能为空");
		}
		return nurseWorkerService.findList(nurseWorkerInfoRequestDto);
	}

	/**
	 * 导出护工Execl
	 */
	@RequestMapping("/exportNurseWorkerInfoExcel")
	public void listNurseWorkerInfoExecl(NurseWorkerInfoRequestDto nurseWorkerInfoRequestDto,
			HttpServletResponse response) {
		SysResult sysResult = nurseWorkerService.listNurseWorkerInfoExcel(nurseWorkerInfoRequestDto);
		try {
			JSONObject jsonObj = (JSONObject) sysResult.getData();
			JSONArray josnArr = jsonObj.getJSONArray("list");
			List<NurseWorkerInfoResponseDto> list = JSONObject.parseArray(josnArr.toJSONString(),
					NurseWorkerInfoResponseDto.class);
			String finalFileName = URLEncoder.encode("护工列表_" + TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS) + ".xls",
					"UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			ExcelUtil.getInstance().exportObj2Excel(toClient, list, NurseWorkerInfoResponseDto.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
